package operator

import (
	"gitee.com/bupt-zkjc/fabric2.2-interface/entity"
)

type ChannelOperator struct {
	FabricOperator *FabricOperator
}

func (l *ChannelOperator) AddChannel(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChainAndChannel(chain, channel)
	if _, err := l.FabricOperator.addChannel(fc); err != nil{
		logger.Error(err)
		return "Add channel failure!", err
	}
	logger.Info("SDK---Add channel success")
	return "Add channel success!", nil
}

func (l *ChannelOperator) CreateChannel(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChainAndChannel(chain, channel)
	if _, err := l.FabricOperator.createChannel(fc); err != nil {
		logger.Error(err)
		return "Create channel failure!", err
	}
	logger.Info("SDK---Create channel success")
	return "Create channel success!", nil
}

func (l *ChannelOperator) JoinPeer(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChannelWithOrg(chain, channel)
	if _, err := l.FabricOperator.joinPeer(fc); err != nil {
		logger.Error(err)
		return "Join peer failure!", err
	}
	logger.Info("SDK---Join peer success")
	return "Join peer success!", nil
}

func (l *ChannelOperator) JoinNewOrgToChannel(chain entity.Chain, channel entity.Channel, flag bool) (
	string, error) {

	fc := entity.ParseFabircChannelWithOrg(chain, channel)
	_, err := l.FabricOperator.joinNewOrgToChannel(fc, flag)
	if err != nil {
		logger.Error(err)
		return "Join new org to channel failure: ", err
	}
	logger.Info("SDK---Join new org to channel success!")
	return "Join new org to channel success! ", nil
}

func (l *ChannelOperator) RemoveExistedOrgFromChannel(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChannelWithOrg(chain, channel)
	_, err := l.FabricOperator.removeExistedOrgFromChannel(fc)
	if err != nil {
		logger.Error(err)
		return "Remove existed org from channel failure: ", err
	}
	logger.Info("SDK---Join new org to channel success")
	return "Join new org to channel success! ", nil
}

func (l *ChannelOperator) JoinNewOrgToSystemChannel(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChannelWithOrg(chain, channel)
	_, err := l.FabricOperator.joinNewOrgToSystemChannel(fc)
	if err != nil {
		logger.Error(err)
		return "Join new org to channel failure: ", err
	}
	logger.Info("SDK---Join new org to system channel success")
	return "Join new org to channel success! ", nil
}

func (l *ChannelOperator) RemoveExistedOrgFromSystemChannel(chain entity.Chain, channel entity.Channel) (string, error) {

	fc := entity.ParseFabircChannelWithOrg(chain, channel)
	_, err := l.FabricOperator.joinNewOrgToSystemChannel(fc)
	if err != nil {
		logger.Error(err)
		return "Remove existed org from channel failure: ", err
	}
	logger.Info("SDk---Remove existed org from channel success")
	return "Remove existed org from channel success! ", nil
}

